package com.example.fy.coolweather.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.example.fy.coolweather.model.Area;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fy on 2016/5/10.
 */
public class CoolWeatherDB {
    private static final String TAG = "CoolWeatherDB";
    /**
     * 数据库名
     */
    public static final String DB_NAME = "cool_weather";

    /**
     * 数据库版本
     */
    public static final int VERSION = 1;

    private static CoolWeatherDB coolWeatherDB;

    private SQLiteDatabase db;
    public static final String path = "/data"+ Environment.getDataDirectory().getAbsolutePath()
            +"/com.example.fy.coolweather/weather";

    /**
     * 构造方法私有化数据库不存在
     */
    private CoolWeatherDB(Context context, InputStream in){
        importToDBFile(in);
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
        Log.d(TAG, "CoolWeatherDB: "+db.toString());
    }

    /**
     * 导入数据到数据库文件中
     */
    public void importToDBFile(InputStream in) {
        FileOutputStream out;
        try{
            Log.d(TAG, "importToDBFile: 导入本地数据");
            out = new FileOutputStream(path);
            byte[] buffer = new byte[400000];
            int count = 0;
            while((count = in.read(buffer)) > 0){
                out.write(buffer,0,count);
            }
            out.close();
            in.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 数据库已经存在
     * @param context
     */
    private CoolWeatherDB(Context context){
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
        Log.d(TAG, "CoolWeatherDB111111: "+db.toString());
    }
    /**
     * 获取CoolWeratherDB实例
     */
    public synchronized static CoolWeatherDB getInstance(Context context,InputStream in){
        if(coolWeatherDB == null){
            Log.d(TAG, "getInstance: 创建db");
            return new CoolWeatherDB(context,in);
        }
        Log.d(TAG, "getInstance: 创建db");
        return coolWeatherDB;
    }

    /**
     * 数据库存在时的单例
     * @param context
     * @return
     */
    public synchronized static CoolWeatherDB getInstance(Context context){
        if(coolWeatherDB == null){
            return new CoolWeatherDB(context);
        }
        return coolWeatherDB;
    }
    /**
     * 保存数据到数据库
     */
    public void saveArea(Area area){
        if (area != null){
            ContentValues values = new ContentValues();
            values.put("areaId",area.getAreaId());
            values.put("nameEn",area.getNameEn());
            values.put("nameCn",area.getNameCn());
            values.put("distinctEn",area.getDistirctEn());
            values.put("distinctCn",area.getDistirctCn());
            values.put("provEn", area.getProvEn());
            values.put("provCn", area.getProvCn());
            values.put("nationEn", area.getNationEn());
            values.put("nationCn", area.getNationCn());
            db.insert("Area", null, values);
        }
    }
    /**
     * 从数据库中读取全国所有省份的信息
     */
    public List<String> loadProvinces(){
        List<String> list = new ArrayList<String>();
        Cursor cursor = db.query(true,"Area",new String[]{"provCn"},null,null,null,null,null,null);
        if(cursor.moveToNext()){
            do{
                list.add(cursor.getString(0));
            }while (cursor.moveToNext());
        }
        if(cursor != null){
            cursor.close();
        }
        return list;
    }
    /**
     * 从数据库中读取某省下的所有地区信息
     */
    public List<String> loadDistinct(String provCn){
        List<String> list = new ArrayList<String>();
        Cursor cursor = db.query(true,"Area",new String[]{"districtCn"},"provCn=?",new String[]{provCn},null,null,null,null);
        if(cursor.moveToNext()){
            do{
                list.add(cursor.getString(0));
            }while (cursor.moveToNext());
        }
        return list;
    }
    /**
     * 从数据库中读取某省下的所有县信息
     */
    public List<String> loadName(String districtCn){
        List<String> list = new ArrayList<String>();
        Cursor cursor = db.query(true,"Area",new String[]{"nameCn"},"districtCn=?",new String[]{districtCn},null,null,null,null);
        if(cursor.moveToNext()){
            do{
                list.add(cursor.getString(0));
            }while (cursor.moveToNext());
        }
        return list;
    }
    /**
     * 从数据库中取出某个县的code
     */
    public String loadAreaId(String nameCn){
        String areaId = "";
        Cursor cursor = db.query(true,"Area",new String[]{"nameCn"},"nameCn=?",new String[]{nameCn},null,null,null,null);
        if(cursor.moveToNext()){
            do{
                areaId =cursor.getString(0);
            }while(cursor.moveToNext());
        }
        if(cursor != null){
            cursor.close();
        }
        return areaId;
    }
}
